﻿using DataVirtualization.Toolkit;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wimm.Common.Entities;
using Wimm.Common.Entities.Utils;
using Wimm.Common.Interfaces.Repositories;
using Wimm.Common.Interfaces.Services;
using Wimm.Common.Interfaces.Utils;

namespace Wimm.UI.ViewModels
{
    public class IncomeViewModel : ViewModelBase
    {
        private PagedGridData<Income, IncomeIcon> _incomeLoader;

        private DateTimeOffset _expensesFromFilter;
        private DateTimeOffset _expensesToFilter;
        private IEnumerable<TimePeriod> _timePeriods;
        private TimePeriod _selectedTimePeriod;

        private DataItemCollection _diagramSource;
        private INavigationService _naviService;
        private IIncomeService _incomeService;
        private IIncomeRepository _incomeRepository;
        private IIconRepository _iconRepository;

        public IncomeViewModel(INavigationService _naviService, IIncomeService _incomeService, IIncomeRepository _incomeRepository, IIconRepository _iconRepository)
        {
            // TODO: Complete member initialization
            this._naviService = _naviService;
            this._incomeService = _incomeService;
            this._incomeRepository = _incomeRepository;
            this._iconRepository = _iconRepository;
            _incomeLoader = new PagedGridData<Income, IncomeIcon>(IncomeRequest);
            _expensesFromFilter = DateTimeOffset.Now;
            _expensesToFilter = DateTimeOffset.Now;
            _timePeriods = TimePeriod.GetConfiguration();
            _selectedTimePeriod = _timePeriods.ElementAt(1);

        }

        private async Task<IPagedResult<IncomeIcon>> IncomeRequest(IPagedRequest<Income> incomeRequest)
        {
            var query = new IncomesQuery(incomeRequest, this.ExpensesFromFilter, this.ExpensesToFilter, null);

            var incomes = await _incomeService.LoadPaged(query);

            var iconsList = new List<IncomeIcon>();

            foreach (Income e in incomes.Data)
            {
                var incomeSource = await _incomeRepository.LoadSourceById(e.IncomeSourceId);

                Icon icon = incomeSource.IconId.HasValue ? await _iconRepository.GetById(incomeSource.IconId.Value) : null;

                iconsList.Add(new IncomeIcon(e, icon, incomeSource.Name));
            }

            var expenseIconPagedResult = new GenericPagedResult<IncomeIcon>(incomes.PageNumber, incomes.PageSize, incomes.TotalPages, iconsList);

            return expenseIconPagedResult;
        }

        public IEnumerable<TimePeriod> TimePeriods
        {
            get { return _timePeriods; }
        }

        public TimePeriod SelectedTimePeriod
        {
            get { return _selectedTimePeriod; }
            set
            {
                Set<TimePeriod>(() => SelectedTimePeriod, ref _selectedTimePeriod, value);
                this.ExpensesFromFilter = _selectedTimePeriod.Start;
                this.ExpensesToFilter = _selectedTimePeriod.End;
            }
        }

        public DateTimeOffset ExpensesFromFilter
        {
            get { return _expensesFromFilter; }
            set { Set<DateTimeOffset>(() => ExpensesFromFilter, ref _expensesFromFilter, value); }
        }

        public DateTimeOffset ExpensesToFilter
        {
            get { return _expensesToFilter; }
            set { Set<DateTimeOffset>(() => ExpensesToFilter, ref _expensesToFilter, value); }
        }

        public DataItemCollection DiagramSource
        {
            get { return _diagramSource; }
            set { Set<DataItemCollection>(() => DiagramSource, ref _diagramSource, value); }
        }

        public PagedGridData<Income, IncomeIcon> IncomeLoader
        {
            get
            {
                return _incomeLoader;
            }
            set
            {
                Set<PagedGridData<Income, IncomeIcon>>(() => IncomeLoader, ref _incomeLoader, value);
            }
        }

    }
}
